package cn.netuo.bumbersoll.upgrade;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.rooten.util.Util;

import java.io.File;

import lib.grasp.helper.DownLoadHelper;
import lib.grasp.util.AppUtil;
import lib.grasp.util.FileUtil;
import lib.grasp.util.L;
import lib.grasp.util.PathUtil;
import lib.grasp.util.PermissionRxUtil;
import lib.grasp.util.PermissionUtil;
import lib.grasp.widget.MessageBoxGrasp;

/**
 * Created by GaQu_Dev on 2018/12/13.
 */
public class AppUpgradeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        doUpGrade(this, getIntent());
    }

    /**
     * 版本升级
     */
    public void doUpGrade(AppCompatActivity activity, Intent intent) {
        if (!PermissionRxUtil.checkDangerousPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) return;
        boolean isSilenceInstall = Util.getBoolean(intent, "isSilenceInstall", true);

        Version version = new Version();
        version.fileName = Util.getString(intent, "fileName");
        version.versionName = Util.getString(intent, "versionName");
        version.size = Util.getLong(intent, "size");
        version.downloadPath = Util.getString(intent, "downloadPath");

        File tempFile = new File(PathUtil.getApkAbsPath(version.fileName, false, version.versionName));
        File saveFile = new File(PathUtil.getApkAbsPath(version.fileName, true, version.versionName));
        if (FileUtil.isFileExists(saveFile.getAbsolutePath())) {
            if (saveFile.length() != version.size) {
                FileUtil.delFile(saveFile); // 删掉之后重新下载
            } else {
                AppUtil.installAPK(activity, saveFile);
                return;
            }
        }

        FileUtil.delDir(tempFile);
        FileUtil.delDir(saveFile);

        DownLoadHelper loadHelper = new DownLoadHelper(activity, version.downloadPath, saveFile.getAbsolutePath());
        loadHelper.setLoadListener(new lib.grasp.helper.LoadListener() {
            @Override
            public void onSuccess() {
                L.logOnly("isSilenceInstall:" + isSilenceInstall);
                L.logOnly("AppUtil.isRoot():" + AppUtil.isRoot());
                if(isSilenceInstall && AppUtil.isRoot()){   // 如果已经root并且要静默安装
                    if (TextUtils.isEmpty(saveFile.getAbsolutePath())) return;
                    AppUtil.installSilence(activity, saveFile.getAbsolutePath());
                }
                else{
                    MessageBoxGrasp.confirmMsg(activity, "确认提示框", "下载完成,现在安装?",
                            v -> {
                                if (TextUtils.isEmpty(saveFile.getAbsolutePath())) return;
                                AppUtil.installAPK(activity, saveFile.getAbsolutePath(), getPackageName() + ".fileprovider");
                                finish();
                            },
                            v -> finish(),
                            true);
                }
            }

            @Override
            public void onFail() {
                MessageBoxGrasp.infoMsg(activity, "下载失败, 请稍候再试");
                finish();
            }
        });
        loadHelper.startLoad();
    }
}
